home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / cexpress / printer / wrapline.asm < prev   
Encoding:
Assembly Source File  |  1989-05-03  |  4.4 KB  |  144 lines

  1. ;void  wrap_line(input_strg,return_string,remainder,line_len)
  2. ;  char *input_strg,*return_string,*remainder;
  3. ;  int  line_len;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _remainder:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME  CS:_TEXT
  10.     PUBLIC  _wrap_line
  11. _wrap_line proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;
  17.     cmp  _memory_model,0    ;near or far?
  18.     jle  begin        ;jump if near
  19.     inc  bp            ;else add 2 to BP
  20.     inc  bp            ;
  21. begin:    jmp  short Z1        ;jump over data
  22. remainder_seg dw ?
  23. remainder_ofs dw ?
  24. return_seg dw ?
  25. return_ofs dw ?
  26. Z1:    cmp  _memory_model,2    ;data near or far?
  27.     jb   Y1            ;jump if near    
  28.     les  di,dword ptr[bp+8] ;ES:DI pts to return string
  29.     lds  bx,dword ptr[bp+12] ;point DS:BX to remainder
  30.     mov  cs:remainder_seg,ds ;save for later
  31.     mov  cs:remainder_ofs,bx ;
  32.     mov  byte ptr ds:[bx],0    ;return null in remainder if error
  33.     lds  si,dword ptr[bp+4]    ;point DS:SI to Strg
  34.     mov  ax,[bp+16]        ;line_len value to AX
  35.     jmp  short X1        ;jump ahead
  36. Y1:    mov  ax,ds        ;NEAR case
  37.     mov  es,ax        ;
  38.     mov  di,[bp+6]        ;
  39.     mov  bx,[bp+8]        ;
  40.     mov  cs:remainder_seg,ds ;save for later
  41.     mov  cs:remainder_ofs,bx ;
  42.     mov  byte ptr ds:[bx],0    ;return null in remainder if error
  43.     mov  si,[bp+4]        ;
  44.     mov  ax,[bp+10]        ;        
  45. X1:    mov  byte ptr es:[di],0    ;return null in return_strg if error
  46.     mov  cs:return_seg,es    ;keep address for later
  47.     mov  cs:return_ofs,di    ;
  48.     or   ax,ax        ;test for null line_length
  49.     jz   U1            ;quit if null
  50.     push si            ;get length of input string
  51.     sub  cx,cx        ;clear CX
  52. W1:    cmp  byte ptr[si],0    ;
  53.     je   V1            ;
  54.     inc  cx            ;
  55.     inc  si            ;
  56.     jmp  short W1        ;
  57. V1:    pop  si            ;
  58.     dec  si            ;
  59.     or   cx,cx        ;test if null input
  60.     jnz  A1            ;
  61. U1:    jmp  O1            ;quit if null
  62. A1:    cmp  byte ptr[si+1],32    ;nxt char a space?
  63.     jne  C1            ;jump ahead if not
  64.     cmp  byte ptr[si+3],32    ;3rd char a space?
  65.     jne  B1            ;jump ahead if not
  66.     cmp  byte ptr[si+2],32    ;2nd char a space?
  67.     je   C1            ;don't del spcs if so
  68. B1:    dec  cl            ;dec string len ctr
  69.     inc  si            ;forward si to eliminate char
  70.     jmp  short A1        ;go check for another spc
  71. C1:    mov  dx,cx        ;copy strg len to DX
  72.     sub  bx,bx        ;use BX as ptr to Strg
  73. D1:    inc  bx            ;forward string pointer
  74.     cmp  byte ptr[si][bx],128   ;low end of codes
  75.     jb   E1            ;jump if not ctrl code
  76.     cmp  byte ptr[si][bx],159   ;high end of codes
  77.     ja   E1            ;jump if not ctrl code
  78.     dec  dx            ;dec string length ctr
  79. E1:    loop D1            ;go count next char
  80.     push bx            ;save pointer
  81.     cmp  dx,ax        ;cmp strg len to linelen
  82.     ja   F1            ;jump ahead if longer
  83.     pop  cx            ;balance stack for jmp
  84.     mov  cx,bx        ;mov string len to CX
  85.     jmp  short M1        ;go set string for return
  86. F1:    mov  cx,ax        ;linelen to CX
  87.     sub  ax,ax        ;AX holds spc positions
  88.     inc  cx            ;count one further
  89.     sub  bx,bx        ;pt SI to start of strg
  90. G1:    inc  bx            ;forward string pointer
  91.     cmp  byte ptr[bx][si],128  ;low end of codes
  92.     jb   H1            ;jump ahead if below
  93.     cmp  byte ptr[bx][si],159  ;high end of codes
  94.     ja   H1            ;jump ahead if above
  95.     jmp  short G1        ;else don't count it
  96. H1:    cmp  byte ptr[bx][si],32  ;spc char?
  97.     jne  I1            ;jump ahead if not
  98.     mov  ax,bx        ;else save spc position
  99. I1:    loop G1            ;go do next char in strg
  100.     cmp  ax,0        ;no spaces at all?
  101.     je   J1            ;jump ahead if so
  102.     mov  bx,ax        ;point SI to last spc
  103.     jmp  short K1        ;jump ahead
  104. J1:    mov  ax,bx        ;set string pointer
  105.     dec  bx            ;set back 1
  106. K1:    pop  cx            ;string length to CX
  107.     sub  cx,bx        ;remainder length
  108.     push ax            ;point ES:DI to remainder
  109.     mov  ax,cs:remainder_seg ;point to remainder
  110.     mov  es,ax         ;
  111.     mov  di,cs:remainder_ofs ;
  112.     pop  ax            ;
  113.     dec  di            ;adjust for loop below
  114.     inc bx            ;forward Strg ptr
  115. L1:    inc  di            ;forward remainder ptr
  116.     mov  dl,[bx][si]    ;get char from end of Strg
  117.     mov  es:[di],dl        ;place in remainder
  118.     inc  bx            ;forward string ptr
  119.     loop L1            ;go get next char
  120.     mov  byte ptr es:[di+1],0 ;set terminator
  121.     mov  cx,ax        ;return string length
  122.     dec  cx            ;adjust
  123. M1:    sub  bx,bx        ;clear string pointer
  124.     mov  ax,cs:return_seg    ;point ES:DI back to return string
  125.     mov  es,ax        ;
  126.     mov  di,cs:return_ofs    ;
  127. N1:    inc  bx            ;forward string ptr
  128.     mov  dl,[bx][si]    ;get char from string
  129.     mov  es:[di],dl        ;write a char
  130.     inc  di            ;forward return strg ptr
  131.     loop N1            ;go do next char
  132.     mov  byte ptr es:[di],0    ;set terminating null
  133. O1:    pop  ds            ;
  134.     pop  si            ;
  135.     pop  di            ;
  136.     pop  bp            ;
  137.     cmp  _memory_model,0    ;quit
  138.     jle  quit        ;
  139.     db   0CBh        ;RET far
  140. quit:    ret            ;RET near
  141. _wrap_line endp        
  142. _TEXT    ENDS
  143.     END
  144.